package com.daedalus.File.Serialization;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Formatter;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
	static Scanner input=new Scanner(System.in);
	static Shitesi shites[]=new Shitesi[100];
	static int no_shitesave=0;
	
	public static void main(String[] args) {
		
		System.out.printf("\n===================================================\n" );
		System.out.printf("Per te perfunduar hyrjen e te dhenave shtypni Ctrl+Z" );
		System.out.printf("\n===================================================\n" );
		
		
		while(true){
			String emri="";
			double rroga=0.0;
			double perqindja=0.0;
			
			try{
				System.out.println("Jepni emrin e shitesit: ");
				emri = input.next();
				System.out.println("Jepni rrogen baze te tij: ");
				rroga=input.nextDouble();
				System.out.println("Jepni perqindjen (0-1) Bonus per shitjet e kryera");
				perqindja=input.nextDouble();
			}
			catch(InputMismatchException io){
				System.err.print("Wrong input");
			}
			catch (NoSuchElementException e){
				System.err.print("Wrong error");
			}
			shites[no_shitesave]=new Shitesi(emri,rroga,perqindja);
			System.out.println("Jepni shitjet per kete shites");
			shtoShitjet();
			no_shitesave++;
			System.out.println("Do shtosh nje punonjes tjeter? (p/j)");
			String answer=input.next();
			System.out.print(answer);
			if(answer.compareTo("jo")==0)
				break;
			
		}

		for(int i=0;i<no_shitesave;i++){
			System.out.printf("\n\n\n"+shites[i]);
			shites[i].printSales();
		}
		
		try {
			writeToFormatter();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			serialOutput();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			deSerial();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void shtoShitjet(){

		while(true){
			try{
			System.out.print("Vlera: ");
			double vlera=input.nextDouble();
			if (vlera==0.0) return;
			shites[no_shitesave].addSale(vlera);
			}catch(InputMismatchException ie){
				System.err.print("vlere jo e sakte");
				return;
			}
		}
	}
	
	private static void writeToFormatter() throws FileNotFoundException{
		Formatter output=new Formatter("punonjesi.txt");
		
		for(int i=0;i<no_shitesave;i++){
				output.format("%s\n", shites[i]);
		}	
		output.close();
	}
	
	private static void serialOutput() throws FileNotFoundException, IOException{
		
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("punonj.ser"));
		
		for(int i=0;i<no_shitesave;i++){
			out.writeObject(shites[i]);
		}
		out.close();
	}
	
	private static void deSerial() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream in=new ObjectInputStream(new FileInputStream("punonj.ser"));
		
		Shitesi shites=(Shitesi) in.readObject();
		System.out.println("deserialized: ");
		System.out.println(shites);
	}
}
